<template>
  <div class="guns-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="名称:">
              <a-input v-model:value.trim="where.name" placeholder="请输入名称" allow-clear />
            </a-form-item>
            <a-form-item label="状态">
              <a-select v-model:value="where.state" placeholder="请选择" allow-clear style="width: 120px;">
                <a-select-option value="Y">已归还</a-select-option>
                <a-select-option value="N">未归还</a-select-option>
              </a-select>
            </a-form-item>
<!--              <a-form-item label="借出时间">
              <a-range-picker class="ele-fluid" value-format="YYYY-MM-DD hh:mm:ss"  v-model:value="where.lendTime" />
              </a-form-item>
              <a-form-item label="归还时间">
              <a-range-picker class="ele-fluid" value-format="YYYY-MM-DD hh:mm:ss"  v-model:value="where.returnTime" />
              </a-form-item> -->
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false" class="table-height">
        <BasicTable :canResize="false" ref="tableRef" :api="FixedAssetsInformationApi.findPage" :where="where"
          :columns="columns" showTableSetting rowKey="id" :rowSelection="{
            type: 'checkbox',
            selectedRowKeys: checkedKeys,
            onChange: onSelectChange,
          }">
          <!-- table上边工具栏 -->
          <template #toolbar>
            <div class="table-toolbar">
              <a-space>
                <a-button type="primary" @click="openEdit()">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新建</span>
                </a-button>
                <a-button type="primary" @click="lend()">
                  <template #icon>
                    <delete-outlined />
                  </template>
                  <span>借出</span>
                </a-button>
                <a-button type="primary" @click="back()">
                  <template #icon>
                    <delete-outlined />
                  </template>
                  <span>归还</span>
                </a-button>
<!--                <a-button danger @click="removeBatch()">
                  <template #icon>
                    <delete-outlined />
                  </template>
                  <span>删除</span>
                </a-button> -->
              </a-space>
            </div>
          </template>

          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex == 'name'">
              <!-- <a @click="openEdit(record)">{{ record.name }}</a> -->
            </template>
            <!-- table操作栏按钮 -->
            <template v-else-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此信息吗？" @confirm="remove(record)">
                  <a class="guns-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <FixedAssetsInformationEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    reactive,
    ref,
    createVNode
  } from 'vue';
  import {
    BasicTable
  } from '/@/components/Table';
  import FixedAssetsInformationEdit from './fixed_assets_information_edit.vue';
  import {
    FixedAssetsInformationApi
  } from '/@/api/fixedAssetsInformation/FixedAssetsInformationApi';
  import {
    message,
    Modal
  } from 'ant-design-vue';
import getDictListData from '/@/utils/dictListData'
const {dictListData} = getDictListData(['returnState']);
  // 查询条件
  const where = reactive({
    name: undefined,
    state: undefined,
    lendTime: undefined,
    returnTime: undefined
  });

  // 表格列配置
  const columns = ref < string[] > ([{
      key: 'index',
      width: 48,
      hideInSetting: true
    },
    {
      title: '名称',
      dataIndex: 'name'
    },
    {
      title: '状态',
      dataIndex: 'state',
      customRender: ({text}) => {
        return dictListData.returnStateObj[text]||'-'
      }
    },
    {
      title: '备注',
      dataIndex: 'memo'
    },
    {
      title: '借出时间',
      dataIndex: 'lendTime',
      width: 170
    },
    {
      title: '归还时间',
      dataIndex: 'returnTime',
      width: 170
    },
    {
      title: '创建时间',
      dataIndex: 'createdTime',
      width: 170
    },
    {
      title: '创建人',
      dataIndex: 'creatoUser'
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      width: 170
    },
    {
      title: '更新人',
      dataIndex: 'updateUser'
    },
    {
      title: '操作',
      key: 'action',
      width: 280,
      align: 'center'
    },
  ]);

  // 多选选中列表
  const checkedKeys = ref < Array < string | number >> ([]);

  // 当前行数据
  const current = ref < any > (null);

  // 是否显示新增编辑弹框
  const showEdit = ref < boolean > (false);

  // ref
  const tableRef = ref(null);

  /**
   * 点击查询
   * @author: nxy
   * @Date: 2022-10-12 09:38:18
   */
  const reload = () => {
    checkedKeys.value = [];
    tableRef.value.reload({
      page: 1
    });
  };

  /**
   * 点击重置
   * @author: nxy
   * @Date: 2022-10-12 09:38:29
   */
  const reset = () => {
    where.name = null;
    where.state = null;
    where.lendTime = null;
    where.returnTime = null;
  };

  // 表格选中改变
  const onSelectChange = (selectedRowKeys: (string | number)[]) => {
    checkedKeys.value = selectedRowKeys;
  };

  /**
   * 打开编辑弹窗
   *
   * @author fengshuonan
   * @date 2021/4/2 17:03
   */
  const openEdit = (row) => {
    current.value = row;
    showEdit.value = true;
  };

  /**
   * 借出
   *
   * @author fengshuonan
   * @date 2021/4/2 17:03
   */
  const lend = () => {
    const data = checkedKeys.value;
    if (!checkedKeys.value.length) {
      message.error('请至少选择一条数据');
      return;
    }FixedAssetsInformationApi.lend({
          id: data[0],
        })
        message.success("请求成功");
        checkedKeys.value = [];
        reload();
  };
  /**
   * 归还
   *
   * @author fengshuonan
   * @date 2021/4/2 17:03
   */
  const back = () => {
    const data = checkedKeys.value;
    if (!checkedKeys.value.length) {
      message.error('请至少选择一条数据');
      return;
    }FixedAssetsInformationApi.back({
          id: data[0],
        })
        message.success("请求成功");
        checkedKeys.value = [];
        reload();
  };
  /**
   * 批量删除
   *
   * @author fengshuonan
   * @date 2021/4/2 17:03
   */
  const removeBatch = () => {
    showEdit.value = false;
    if (!checkedKeys.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的信息吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: async () => {
        let params = checkedKeys.value;
        const result = await FixedAssetsInformationApi.batchDel({
          ids: params
        });
        message.success('删除成功');
        reload();
      },
    });
  };

  /**
   * 删除单个
   *
   * @author fengshuonan
   * @date 2021/4/2 17:03
   */
  const remove = async (row: any) => {
    const result = await FixedAssetsInformationApi.delete({
      id: row.id
    });
    message.success('删除成功');
    reload();
  };
</script>
